﻿using System;
using System.Collections.Generic;
using System.Security;
using Microsoft.Practices.Web.UI.WebControls;
using TaskKernel.Domain;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IAdminManagersView : IAbstractView
    {
        Guid User { get; }
        List<Account> Managers { set; }
        List<Account> Accounts { set; }
        string Message { set; }
        List<Account> Slavers { set; }
    }

    public partial class AdminManagers : ViewPage<IAdminManagersView, AdminManagersPresenter>, IAdminManagersView
    {
        #region IAdminManagersView Members

        public string DepartmentName
        {
            set { Department.Text = value; }
        }

        public new Guid User
        {
            get { return new Guid(Request.QueryString["ID"]); }
        }

        public List<Account> Managers
        {
            set
            {
                ObjectContainerDataSource1.DataObjectTypeName = typeof (Account).AssemblyQualifiedName;
                ObjectContainerDataSource1.DataSource = value;
                ObjectContainerDataSource1.DataBind();
            }
        }

        public List<Account> Accounts
        {
            set
            {
                AccountDDL.DataSource = value;
                AccountDDL.DataBind();
            }
        }

        public string Message
        {
            set { Label1.Text = value; }
        }

        public List<Account> Slavers
        {
            set
            {
                ObjectContainerDataSource2.DataObjectTypeName = typeof (Account).AssemblyQualifiedName;
                ObjectContainerDataSource2.DataSource = value;
                ObjectContainerDataSource2.DataBind();
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                Presenter.OnViewInitialized();
            Presenter.RefreshView();
        }

        protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var user = (Account) e.Instance;
            try
            {
                Presenter.OnDeleteManager(user);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Presenter.OnAddManager(new Guid(AccountDDL.SelectedItem.Value));
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            try
            {
                Presenter.OnAddSlave(new Guid(AccountDDL.SelectedItem.Value));
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }

        protected void ObjectContainerDataSource2_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var user = (Account) e.Instance;
            try
            {
                Presenter.OnDeleteSlave(user);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.RefreshView();
            }
        }
    }
}